package com.gegao.somethinginteresting.activity.base;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.gegao.somethinginteresting.IntertstingApplication;
import com.gegao.somethinginteresting.R;
import com.gegao.somethinginteresting.injects.ActivityModule;
import com.gegao.somethinginteresting.injects.ApplicationComponent;

/**
 * Created by liuchenghui on 2016/03/15.
 */
public class BaseActivity extends AppCompatActivity {

    private static final int BASE_VIEW_ID = R.layout.activity_base;
    private static final ViewGroup.LayoutParams LAYOUT_PARAMS = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    private LinearLayout mParentView;
    private Toolbar mToolBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getAppComponent().inject(this);//为dragger2 注入baseActivity
        setContentView(BASE_VIEW_ID);
    }

    public void setContentView(int layoutResID) {
        if (BASE_VIEW_ID == layoutResID) {
            super.setContentView(layoutResID);
            mParentView = (LinearLayout) findViewById(R.id.ll_parent);
            mToolBar = (Toolbar) findViewById(R.id.toolbar);
            initToolbar(mToolBar);
            return;
        }

        mParentView.addView(getLayoutInflater().inflate(layoutResID, null), LAYOUT_PARAMS);
    }

    @Override
    public void setContentView(View view) {
        mParentView.addView(view, LAYOUT_PARAMS);
    }

    private void initToolbar(Toolbar toolbar) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    ;

    public Toolbar getToolBar() {
        return mToolBar;
    }

    public void setBackground(int colorId) {
        if (null != mParentView) {
            mParentView.setBackgroundColor(getResources().getColor(colorId));
        }
    }

    protected ApplicationComponent getAppComponent() {
        return ((IntertstingApplication) getApplication()).getApplicationComponent();
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }
}
